<?php

namespace App\Http\Controllers;

use App\Models\Region;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class DemoController extends BaseController
{
    public function test()
    {
        /** @var User $user */
        $user = Auth::user();

        if (empty($user)) return self::responseMsg("获取登录用户失败");

        return self::responseJson($user->groups()->pluck('flag'));
    }

    public function region()
    {
//        $regions = Region::with('regions')->where('code', '340000')->first();

        $list = [];
        $regions = Region::query()->where('level', 0)->get();

        /** @var Region $province */
        foreach ($regions as $province) {
            $cities = [];
            /** @var Region $city */
            foreach ($province->children()->get() as $city) {
                $districts = [];
                /** @var Region $district */
                foreach ($city->children()->get() as $district) {
                    $districts[] = [
                        'value' => $district->code,
                        'label' => $district->name,
                    ];
                }
                $cities[] = [
                    'value' => $city->code,
                    'label' => $city->name,
                    'children' => $districts
                ];
            }
            $list[] = [
                'value' => $province->code,
                'label' => $province->name,
                'children' => $cities
            ];
        }

        return self::responseJson($list);
    }
}
